<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
   <%@ include file="/common/CimsCommon.jsp" %>
   <link rel="stylesheet" href="<%=basePath%>/css/cims/cmmBase.css">
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
   <script type="text/javascript" src="${basePath}/js/cims/transfereeDeal/monthSettle/addTransfereeSettle.js"></script>
   
</head>
  
<body style="background-color:#FFFFFF">
<div id="formPageSwap">
  <br/>
<form id="addNewsFormData" name="addNewsFormData" isCheck="true" action="com.jy.modules.common.tcmmquota.controller.TCMmQuotaController">
	<%@include file="commonCreditSettle.jsp" %>
		<div class="expandToolbar">
			<span class="submitBtn" id="saveSettle" onclick="doAddFrom();">保存</span>
			<span class="submitBtn" onclick="parent.tabs.close();">关闭</span>
		</div>
	</div>
<!-- 保存 关闭 按钮 在 查询页面进行控制 -->  
</form>

</div>
</body>

<script type="text/javascript">
   var doAddFrom;
   var doContract;
   var selectOptions=[];
  
   $(document).ready(function(){
   		checkedInit();//平台校验不兼容IE
   		Cmm.util.dateInputInit("addNewsFormData");

   		
   		
   		var select=$("#settlePackage"),
   		    selectSettleDate=$("#idSettleDate"),
   		    table;
   		/* select.on("change",function(){
   			var value=select.val(),
			settleDate=selectSettleDate.val();
   			value=value?value:"000";
   			settleDate=settleDate?settleDate:"000";
   			
 			  if(value!="000"&&settleDate!="000"){
 			  	selectChange(select,selectSettleDate,table);
 			  }
	          
   		}); */
   		
   		selectSettleDate.on("change",function(){
   			var value=$("#selectPackage").val(),
			settleDate=selectSettleDate.val();
   			value=value?value:"000";
   			settleDate=settleDate?settleDate:"000";
   			
	          if(value!="000"&&settleDate!="000"){
   			  	selectChange(select,selectSettleDate,table);
   			  }
   		});
   		doContract=function (contractCode){
   			var url=contextRootPath+"/tCMmQuota/queryOneContractAll?contractCode="+contractCode;
   		    if(table){
   		    	table.url=table.url.split("=")[0]+"=000"+"&dealType="+cimsConstant.DEAL_TYPE_CREDIT_SETTLE;
   		    	table.query();
   		    }
   				jyAjax(url,null,function(msg){
   				//保存成功后，执行查询页面查询方法
   		    	var v_status = msg.status;
   		    	if(v_status.indexOf('ok') >-1){
   		    		var cdto=msg.data.cdto,
   		    			cspDto=msg.data.cspDto,
   		    			cooperorgDTO=msg.data.cooperorgDTO,
   		    			agreementDTO=msg.data.agreementDTO,
   		    			url;
   					table=iframe.iframeObj["table"];
   					url=table.url;
   		    		
   		    		$("input[name=contractCode]").val(cdto.contractCode);
   		    		$("input[name=contractName]").val(cdto.contractName);
   		    		$("input[name=cooperorgCode]").val(cdto.cooperorgCode);
   		    		$("input[name=cooperorgName]").val(cooperorgDTO.cooperorgName);
   		    		$("input[name=agreementCode]").val(cdto.agreementCode);
   		    		$("input[name=agreementName]").val(agreementDTO.agreementName);
   		    		$("select[name=beforeSettleType]").val(cspDto&&cspDto.beforeSettleType);
   		    		$("select[name=overdueType]").val(cspDto&&cspDto.overdueType);
   		    		$("select[name=replaceType]").val(cspDto&&cspDto.replaceType);
   		    		url="${basePath}cooperorgDeal/getPackageList?cooperorgCode="+cdto.cooperorgCode+"&contractCode="+contractCode + "&pagType=" + cimsConstant.CREDIT_RESOURCE;
   		    		jyAjax(url,null,function(msg){
   		    			//保存成功后，执行查询页面查询方法
   		    			var v_status = msg.status;
   		    			if(v_status.indexOf('ok') >-1){
   		    				var array=["<option value=''>--请选择--</option>"],data=msg.data;
   		    				data=data?data:[];
   		    				$.each(data,function(i,n){
   		    					array.push("<option value='"+n.creditPackageName+"'>"+n.creditPackageName+"</option>");
   		    				selectOptions.push(n.creditPackageName);
			
	        				});
	        				select.empty();
	        				select.append(array.join(""));
	        				var aa = table.url;
	        				completePackage(selectOptions,"settlePackage","","",table);
   		    			}
   		    		});
   		    	}
   			});
   		}
   		
   		/* !$("#dealCode").val()&&importBtn.trigger("click"); */

		doAddFrom=function() {
			if (!CheckUtil.formCheck("addNewsFormData")) {
				return;
			}
			selectPackage = $("#selectPackage");
			
			if(!selectPackage.val()){
				alert("请选择债权包");
				return;
			}
			
			var packageName = selectPackage.val();
			var textValue = $("#settlePackage option[value="+packageName+"]").length;
	
			if(textValue<=0){
				alert("包名在选项列表中不存在！");
				return;
			}
			
			if(confirm("请仔细核对信息，再确认保存！")){
			// 序列化 新增页面的form表单数据
			var params = $("#addNewsFormData").serialize();
			params += "&dealType="+cimsConstant.DEAL_TYPE_CREDIT_ABNORMAL_SETTLE_REC;
			var url = contextRootPath + '/transfereeSettle/saveTransfereeMonthSettle';
			// 通过ajax保存
			jyAjax(url, params, function(msg) {
				// 新增成功后，
				// alert(msg.msg);
				var v_status = msg.status;
				if (v_status.indexOf('ok') > -1) {
					// 新增成功后 刷新页面 或 只查询 id为msg.data['id'] 的 数据
					try{
						parent.tabs.getTabWinByTitle("异常债权交易").queryData();
					}catch(e){
					
					}finally{
						parent.tabs.close();
						
					}
				}
			});
			}
			
		};

	});
	function selectChange(select,selectSettleDate,table){
		var contractCode= $("#dtocontractCode").val();
  		var cooperorgCode= $("#dtocooperorgCode").val();
		var value=$("#selectPackage").val(),
			settleDate=selectSettleDate.val();
   			value=value?value:"000";
   			
			var textValue = $("#settlePackage option[value="+value+"]").length;
	
			if(textValue<=0){
				alert("包名在选项列表中不存在！");
				return;
			}
   			
   			table=iframe.iframeObj["table"];
   			table.url=table.url.split("=")[0]+"="+value+"&contractCode="+contractCode+"&cooperorgCode="+cooperorgCode+"&dealType="+cimsConstant.DEAL_TYPE_CREDIT_NORMAL_SETTLE_REC + "&settleDate="+settleDate;
   			table.query();
   			

            $("#thisRepaySum").val("");//结算金额
            $("#matchAmount").val("");//需匹配金额
            $("#returnAmount").val("");//返还金额
            $("#creditRows").val("");//债权数目
            $("#practicalSellSum").val("");//实际匹配金额
   			var dataUrl =contextRootPath + "/settle/getSettleInfo";
	        $.ajax({
		        url: dataUrl,
		        type: 'get',
		        dataType: 'json',
		        data:{
		            "contractCode":contractCode,
		            "cooperorgCode":cooperorgCode,
		            "dealType": cimsConstant.DEAL_TYPE_CREDIT_NORMAL_SETTLE_REC,
		            "packageNum": value,
		            "settleDate": settleDate
		            },
		        success: function(results) {
			          var obj = results.data;
			          if(obj!=null){
				          $("#thisRepaySum").val(obj.thisRepaySum);//结算金额
				          $("#matchAmount").val(obj.matchAmount);//需匹配金额
				          $("#returnAmount").val(obj.returnAmount);//返还金额
				          $("#creditRows").val(obj.creditRow);//债权数目
				          $("#practicalSellSum").val(obj.practicalAmount);//实际匹配金额
			          }
		       }
	          });
	}
	//选择合约
    toAddContract();
	/**
	**  异常受让结算导出
	**/
    function exportCreditSettle(){
 		if(!$("#selectPackage").val()||!$("#idSettleDate").val()){
 			alert("债权包和结算日期不能空！");
 			return;
 		}
 		var url=contextRootPath+"/export/exportSettle?contractCode="+$("#dtocontractCode").val()+"&packageNum="
 			+$("#selectPackage").val()+"&templateType="+cimsConstant.DEAL_TYPE_MONTH_BACK
 			+"&dealCode="+$("#dealCode").val()+"&cooperorgCode="+$("#dtocooperorgCode").val() +"&dealType="+cimsConstant.DEAL_TYPE_CREDIT_ABNORMAL_SETTLE_REC
 			+"&settleFlag="+$("#settleFlag").val()  + "&settleDate=" + $("#idSettleDate").val();
 		Cmm.download($("#dtocontractCode").val(),cimsConstant.DEAL_TYPE_MONTH_BACK,url);
 	}
</script>
 <%@include file="queryTCMmQuotaCreditDetail.jsp" %>
</html>
